<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/12/27
 * Time: 14:45
 */
namespace app\admin\controller;
use think\Db;
class Province extends Base {
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($role)&&$role != ""){
                $map['ag.id'] = $role;
            }
            if(isset($key)&&$key!="")
            {
                $map['a.p_username|a.p_account'] = ['like',"%" . $key . "%"];
            }
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_province')->alias('a')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_province')
                ->alias ('a')
                ->page($page, $limit)
                ->where($map)
                ->select();
            foreach ($list as $key=>&$vo){
                $where['district_id'] = array('IN',trim($vo['p_range'],','));
                $district = Db::table('up_area')->where($where)->column('district');
                $vo['range'] = implode(',',$district);
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }

        return $this->fetch();
    }

    /**
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $id = input('param.id');
        Db::startTrans();// 启动事务
        $data = Db::table('up_factory')->where('f_id',$id)->find();
        if($data['factory_id']!=''){
            return json(['code' => 100, 'data' => '', 'msg' => '当前账号下有客户信息,不能删除']);
        }
        $res = Db::table('up_province')->where('p_id',$id)->delete();
        Db::table('up_banner_hold')->where('b_user',$id)->delete();//删除待机广告
        //启动广告
        $data1 = Db::table('up_banner_start')->where('b_user',$id)->find();
        if($data1['b_id']!=''){
            Db::table('up_banner_start')->where('b_user',$id)->delete();//删除启动广告
        }
        $data2 = Db::table('up_notice')->where('n_user_id',$id)->find();
        if($data2['n_id']!=''){
            Db::table('up_notice')->where('n_user_id',$id)->delete();//删除消息
        }

        if($res){
            Db::commit();
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            Db::rollback();
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }
    /***
     * @return mixed
     * 添加
     */
    public function add(){
        if(request()->isPost()){
            $province = Db::table('up_province')->where('p_username',input('param.p_username'))->find();
            if($province['p_id']){
                return json(['code' => 100, 'data' => '', 'msg' => '该管理员名称已经存在,请重新填写管理员名称']);
            }

            if(mb_strlen(input('param.p_password'),'UTF8')>16){
                return json(['code' => 100, 'data' => '', 'msg' => '密码最多不能超过16位']);
            }
            if(mb_strlen(input('param.p_username'),'UTF8')>30){
                return json(['code' => 100, 'data' => '', 'msg' => '账号最多可输入30字符']);
            }
            $data['p_account'] = 'S'.rand(100000,999999);
            $data['p_username'] = input('param.p_username');
            $data['p_password'] = md5(input('param.p_password'));
            $data['p_pass'] = input('param.p_password');
            $data['p_range'] = ','.input('param.p_range').',';
            $data['p_create_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_province')->insertGetId($data);
            for ($i=0;$i<4;$i++){
                $result['b_user'] = $res;
                $result['b_time'] = date('Y-m-d H:i:s',time());
                $result['b_update_time'] = date('Y-m-d H:i:s',time());
                $resultAll[] = $result;
            }
            Db::table('up_banner_hold')->insertAll($resultAll);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $data = Db::table('up_area')->where('level',1)->select();
        $this->assign('data',$data);
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(request()->isPost()){
            $id = input('param.id');
            $where['p_id'] = array('NEQ',$id);
            $where['p_username'] = input('param.p_username');
            $result = Db::table('up_province')->where($where)->find();
            if($result['p_id']){
                return json(['code' => 100, 'data' => '', 'msg' => '该管理员名称已经存在,请重新填写管理员名称']);
            }
            if(mb_strlen(input('param.p_password'),'UTF8')>16){
                return json(['code' => 100, 'data' => '', 'msg' => '密码最多不能超过16位']);
            }
            if(mb_strlen(input('param.p_username'),'UTF8')>30){
                return json(['code' => 100, 'data' => '', 'msg' => '账号最多可输入30字符']);
            }
            $dataAll['p_username'] = input('param.p_username');
            $dataAll['p_password'] = md5(input('param.p_password'));
            $dataAll['p_pass'] = input('param.p_password');
            $dataAll['p_range'] = ','.input('param.p_range').',';
            $map['p_id'] = $id;
            $aa = Db::table('up_province')->where($map)->update($dataAll);
            if($aa){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_province')->where('p_id',$id)->find();
        $res['p_range'] =explode(',',trim($res['p_range'],','));
        $data = Db::table('up_area')->where('level',1)->select();
        $this->assign('data',$data);
        $this->assign('res',$res);
        return $this->fetch();
    }

}